//
//  MenuViewController.m
//  MenuDemo
//
//  Created by 卢浩 on 16/4/14.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "MenuViewController.h"

//默认抽屉的宽度
#define KDEFAULTWIDHT [UIScreen mainScreen].bounds.size.width * 0.4

@interface MenuViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *leftTableView;

@property (nonatomic,strong) UITableView *rightTableView;

@end

@implementation MenuViewController

static MenuViewController *menuVC = nil;

/**
 *  类方法:初始化抽屉类
 */
+ (instancetype)initWithCustomLeftVC:(UIViewController *)leftVC leftWidth:(CGFloat)leftWidth CustomRightVC:(UIViewController *)rightVC rightWidth:(CGFloat)rightWidth CenterVC:(UIViewController *)centerVC{
    
    //线程锁
    @synchronized (menuVC) {
        //初始化抽屉类
        if (menuVC == nil) {
            menuVC = [[MenuViewController alloc]init];
        }
        //设置window的根视图
        [UIApplication sharedApplication].keyWindow.rootViewController = menuVC;
        //判断三个参数是否为空,不为空分别赋值,并将控制器及视图添加到抽屉类上
        if (leftVC) {
            menuVC.customLeftVC = leftVC;
            menuVC.customLeftWidht = leftWidth;
            [menuVC addChildViewController:leftVC];
            [menuVC.view addSubview:leftVC.view];
        }else{
            [menuVC addDefaultLeftMenu];
        }
        if (rightVC) {
            menuVC.customLeftVC = rightVC;
            menuVC.customRightWidth = rightWidth;
            [menuVC addChildViewController:rightVC];
            [menuVC.view addSubview:rightVC.view];
        }else{
            [menuVC addDefaultRightMenu];
        }
        if (centerVC) {
            menuVC.centerVC = centerVC;
            [menuVC addChildViewController:centerVC];
            [menuVC.view addSubview:centerVC.view];
        }
        //设置中心视图显示在最上方
        [menuVC.view bringSubviewToFront:menuVC.centerVC.view];
    }
    return menuVC;
}

/**
 *  添加默认左侧抽屉
 */
- (void)addDefaultLeftMenu{
    //初始化
    menuVC.leftTableView = [[UITableView alloc]initWithFrame:CGRectMake(-KDEFAULTWIDHT, 0, KDEFAULTWIDHT, menuVC.view.frame.size.height) style:UITableViewStylePlain];
    //代理
    menuVC.leftTableView.delegate = menuVC;
    //数据源
    menuVC.leftTableView.dataSource = menuVC;
    //弹性关闭
    menuVC.leftTableView.bounces = NO;
    //行间隔线样式
    menuVC.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //添加到视图上
    [menuVC.view addSubview:menuVC.leftTableView];
}

/**
 *  添加默认右侧抽屉
 */
- (void)addDefaultRightMenu{
    CGSize viewSize = self.view.frame.size;
    self.rightTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, viewSize.width, viewSize.height) style:UITableViewStylePlain];
    self.rightTableView.delegate = self;
    self.rightTableView.dataSource = self;
    self.rightTableView.bounces = NO;
    self.rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.rightTableView];
}

- (void)setLeftDataArr:(NSMutableArray *)leftDataArr{
    if (_leftDataArr != leftDataArr) {
        _leftDataArr = nil;
        _leftDataArr = leftDataArr;
        [self.leftTableView reloadData];
    }
}

- (void)setRightDataArr:(NSMutableArray *)rightDataArr{
    if (_rightDataArr != rightDataArr) {
        _rightDataArr = nil;
        _rightDataArr = rightDataArr;
        [self.leftTableView reloadData];
    }
}

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == menuVC.leftTableView) {
        return menuVC.leftDataArr.count;
    }else{
        return menuVC.rightDataArr.count;
    }
}

//行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    if (tableView == menuVC.leftTableView) {
        cell.textLabel.text = menuVC.leftDataArr[indexPath.row];
    }else{
        cell.textLabel.text = menuVC.rightDataArr[indexPath.row];
    }
    return cell;
}

//点击行事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //调用协议方法,并隐藏抽屉
    if (tableView == self.leftTableView) {
        [self.clickMenuCellDelegate didClickLeftMenuCellTitle:self.leftDataArr[indexPath.row]];
        [self hideLeftVC];
    }else{
        [self.clickMenuCellDelegate didClickRightMenuCellTitle:self.rightDataArr[indexPath.row]];
        [self hideRightVC];
    }
    
}

//区头部高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 64;
}

//区头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (tableView == menuVC.leftTableView) {
        return @"菜单页";
    }else{
        return @"用户页";
    }
}


//判断左侧抽屉是否显示
- (BOOL)isLeftShow{
    if (menuVC.leftTableView.frame.origin.x < 0) {
        return NO;
    }else{
        return YES;
    }
}

//判断右侧抽屉是否显示
- (BOOL)isRightShow{
    if (menuVC.centerVC.view.frame.origin.x < 0) {
        return YES;
    }else{
        return NO;
    }
}

/**
 *  单例:获取抽屉类
 */
+ (instancetype)getMenuVC{
    return menuVC;
}

/**
 *  展示左侧抽屉
 */
- (void)showLeftVC{
    //动画显示
    [UIView animateWithDuration:1 animations:^{
        menuVC.centerVC.view.transform = CGAffineTransformMakeTranslation(KDEFAULTWIDHT, 0);
        //判断默认抽屉存在够,移动相应的视图
        if (!menuVC.leftTableView) {
            //不存在,将自定义抽屉移到中心视图下一层
            [menuVC.view insertSubview:menuVC.customLeftVC.view belowSubview:menuVC.centerVC.view];
            //移动自定义抽屉
            menuVC.customLeftVC.view.transform = CGAffineTransformMakeTranslation(menuVC.customLeftWidht, 0);
        }else{
            //存在,将默认抽屉移动中心视图下一层
            [menuVC.view insertSubview:menuVC.leftTableView belowSubview:menuVC.centerVC.view];
            //定义默认抽屉
            menuVC.leftTableView.transform = CGAffineTransformMakeTranslation(KDEFAULTWIDHT, 0);
        }
    }];
}

/**
 *  隐藏左侧抽屉
 */
- (void)hideLeftVC{
    [UIView animateWithDuration:1 animations:^{
        menuVC.centerVC.view.transform = CGAffineTransformMakeTranslation(0, 0);
        if (!menuVC.leftTableView) {
            menuVC.customLeftVC.view.transform = CGAffineTransformMakeTranslation(0, 0);
        }else{
            menuVC.leftTableView.transform = CGAffineTransformMakeTranslation(0, 0);
        }
    }];
}

/**
 *  展示右侧抽屉
 */
- (void)showRightVC{
    CGFloat viewWidth = self.view.frame.size.width;
    [UIView animateWithDuration:1 animations:^{
        self.centerVC.view.transform = CGAffineTransformMakeTranslation(-KDEFAULTWIDHT, 0);
        if (!self.rightTableView) {
            [self.view insertSubview:self.customRightVC.view belowSubview:self.centerVC.view];
            self.customRightVC.view.transform = CGAffineTransformMakeTranslation((viewWidth - KDEFAULTWIDHT), 0);
        }else{
            [self.view insertSubview:self.rightTableView belowSubview:self.centerVC.view];
            self.rightTableView.transform = CGAffineTransformMakeTranslation((viewWidth - KDEFAULTWIDHT), 0);
        }
    }];
}

/**
 *  隐藏右侧抽屉
 */
- (void)hideRightVC{
    [UIView animateWithDuration:1 animations:^{
        self.centerVC.view.transform = CGAffineTransformMakeTranslation(0, 0);
        if (!self.rightTableView) {
            self.customRightVC.view.transform = CGAffineTransformMakeTranslation(0, 0);
        }else{
            self.rightTableView.transform = CGAffineTransformMakeTranslation(0, 0);
        }
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

//获取开始触摸时的坐标
static CGPoint startPoint;
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    startPoint = [touch locationInView:self.view.superview];
}

//获取触摸结束后的坐标
static CGPoint endPoint;
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    endPoint = [touch locationInView:self.view.superview];
    if (self.isLeftShow || self.isRightShow) {
        [self hideLeftVC];
        [self hideRightVC];
    }else{
        //判断平移方向
        if (endPoint.x - startPoint.x > KDEFAULTWIDHT * 0.5) {
            [self showLeftVC];
        }else if (startPoint.x - endPoint.x > KDEFAULTWIDHT *0.5){
            [self showRightVC];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
